package cz.muni.fi.pb162.project.geometry;

import java.util.Collections;
import java.util.Collection;
import java.util.List;
import java.util.ArrayList;
import java.util.Arrays;

/**
 * Write a description of class CollectionPolygon here.
 * 
 * @author Martin Pasko 
 * @version 19_11_2012
 */
public class CollectionPolygon extends SimplePolygon {

    private List<Vertex2D> vertices;
    
    /**
     * Constructs a Polygon with vertices stored in the form of ArrayList Collection
     * @param verts array of vertices
     * @throws IllegalArgumentException if the array or any of its elements is null
     */
    public CollectionPolygon(Vertex2D[] verts) {
        
        if(verts == null) {
            throw new IllegalArgumentException("Pole nebolo inicializovane");
        }    
        
        for(int i=0; i< verts.length; i++) {
            if(verts == null) {
                throw new IllegalArgumentException("Element c." +i+" nebol inicializovany.");
            }
        }    
            
        vertices = new ArrayList<Vertex2D>(Arrays.asList(verts));
    }
    
    public Vertex2D getVertex(int index) {
        if(index < 0) {
            throw new IllegalArgumentException("Index musi byt nezaporne cislo");
        }
        return vertices.get(index%getNumVertices());
    }

    public int getNumVertices() {
        return vertices.size();
    }
    
    /**
     * Returns unmodifiable collection of vertices
     * @return all vertices
     */
    public Collection<Vertex2D> getVertices() {
        return new ArrayList<Vertex2D>(vertices);
    }
    
    /**
     * Inverts the order of vertices in the polygon
     * @return polygon with reversed vertices
     */
    public CollectionPolygon invert() {
        CollectionPolygon colPol = new CollectionPolygon((Vertex2D[])(vertices.toArray()));
        Collections.reverse(colPol.vertices);
        return colPol;
    }
}
